<?
session_start();
include_once("secure/connect.php");
include_once("secure/functions.php");
if (!$_SESSION['logged'])
	{
	print "SESSION FERMEE";
	die;
	}
$sql_look_fiche_loc="SELECT * FROM fiches_loc WHERE id='".$_GET['id_fiche_loc']."'";
$res_look_fiche_loc=mysql_query($sql_look_fiche_loc,$db);
$ligne_look_fiche_loc=mysql_fetch_array ($res_look_fiche_loc);

$sql_look_contact="SELECT * FROM contacts WHERE id='".$ligne_look_fiche_loc['client']."'";
$res_look_contact=mysql_query($sql_look_contact,$db);
$ligne_look_contact=mysql_fetch_array ($res_look_contact);

$sql_look_etat="SELECT impression FROM fiches_loc_etats WHERE id='".$ligne_look_fiche_loc['etat']."'";
$res_look_etat=mysql_query($sql_look_etat,$db);
$ligne_look_etat=mysql_fetch_array ($res_look_etat);
$etat=$ligne_look_etat['impression'];

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Impression fiche location</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../site_print.css" rel="stylesheet" type="text/css">

<style type="text/css">
<!--
.Style1 {font-size: x-small}
.codebare {

}
.coche {
	font-size: xx-small;
}
-->
</style>
</head>

<body>
<table width="630" border="0">
  <tr>
    <td width="257"><div align="center"><img src="../images/FFFFFF/entete_djr.gif" height="82"></div></td>
    <td width="357" align="right" valign="top">Loc <? echo "<span class=\"\">".$ligne_look_fiche_loc['id']."</span>"; ?> - 
    Cli <? echo "<span class=\"\">".$ligne_look_contact['id']."</span>"; ?>
	<?
	$sql_code="SELECT * FROM fiches_devis WHERE id_fiche_loc='".$ligne_look_fiche_loc['id']."'";
	$res_code=mysql_query($sql_code,$db);
	$ligne_code=mysql_fetch_array($res_code);
	if ($ligne_code['code'])
		{
		print "<br>Code location: ".$ligne_code['code'];
		}
	?></td>
  </tr>
  <tr>
  		<td height="28" colspan="2"><div align="center" class="Style1"><strong>CONTRAT DE LOCATION </strong></div></td>
  </tr>
</table>

<table width="630" border="0">
		<tr>
				<td width="460" height="52">&nbsp;</td>
				<td width="250">
				<?
				
				if ($ligne_look_contact['societe']) { echo $ligne_look_contact['societe']."<br>";}
				echo $ligne_look_contact['titre']." ".$ligne_look_contact['nom']." ".$ligne_look_contact['prenom']."<br>";
				echo $ligne_look_contact['rue']."<br>";
				if ($ligne_look_contact['pays']!='CH') echo $ligne_look_contact['pays']."-";
				echo $ligne_look_contact['npa']." ".$ligne_look_contact['lieu'];
				?><p>Les Avanchets, le <? echo date("d.m.Y"); ?></p>
				</td>
		</tr>
</table>
<p><? echo $etat." n&deg;".$ligne_look_fiche_loc['id'];?><br>
  Dates &eacute;v&eacute;nement: du <? echo format_date($ligne_look_fiche_loc['date_debut'],0);?> au <? echo format_date($ligne_look_fiche_loc['date_fin'],0);?><br>
		Dates sortie: du <? echo format_date($ligne_look_fiche_loc['date_out'],0);?> au <? echo format_date($ligne_look_fiche_loc['date_in'],0);?></p>
<img src="../images/separator.gif" width="500" height="2"><br>
<br>
<table width="630" border=0 cellpadding="1" >
<tr>
  <td width="80"><strong>Q. x Co.</strong></td>
  <td width="250"><strong>Libellé</strong></td>
  <td width="65"><strong>SOR</strong></td>
  <td width="65"><strong>RET</strong></td>
  <td width="80" align="right"><strong>Prix</strong></td>
  <td width="80" align="right"><strong>Total</strong></td>
</tr>
<?

	
$sql_look_fiche_loc_items=
"SELECT
fli.id,
fli.libelle,
fli.id_inventaire_description,
descr.nom_court,
fli.id_kit,
kit.nom_court,
fli.quantite,
fli.coef,
fli.prix, 
fli.sous_traitance,
fli.service,
fli.categorie,
ic.nom as cat_nom,
ic.image as cat_img,
ic.ordre as cat_ordre,
fli.type,
it.nom as typ_nom,
it.image as typ_img,
it.ordre as typ_ordre,
fli.hidden,
fli.rabais,
fli.invisible,
fli.ordre

FROM 
fiches_loc_items fli
LEFT OUTER JOIN inventaire_categorie ic ON ic.id=fli.categorie
LEFT OUTER JOIN inventaire_type  it ON it.id=fli.type
LEFT OUTER JOIN inventaire_description descr ON descr.id=fli.id_inventaire_description
LEFT OUTER JOIN kits kit ON kit.id=fli.id_kit


WHERE 
fli.id_fiche_loc='".$ligne_look_fiche_loc['id']."' AND 
invisible is null

ORDER BY 
fli.ordre,
cat_ordre,
typ_ordre";

$res_look_fiche_loc_items=mysql_query($sql_look_fiche_loc_items,$db);
while($ligne_look_fiche_loc_items=mysql_fetch_array ($res_look_fiche_loc_items))
	{
		if (substr($ligne_look_fiche_loc_items['libelle'],0,4)=="----")
			{
			$prix=0;
			print '<tr><td colspan="6"><img src="../images/separator.gif" width="500" height="1"><br>';
			}
		elseif (substr($ligne_look_fiche_loc_items['libelle'],0,3)=="---")
			{
			$prix=0;
			$lib=substr($ligne_look_fiche_loc_items['libelle'],3);
			print '<tr><td colspan="6"><b>'.$lib.'</b></td></tr>';
			}
	else
		{
		$prix=$ligne_look_fiche_loc_items['coef']*$ligne_look_fiche_loc_items['quantite']*$ligne_look_fiche_loc_items['prix']*(1-($ligne_look_fiche_loc_items['rabais']/100));
		print "<tr><td>".(1*$ligne_look_fiche_loc_items['quantite'])." x ".(1*$ligne_look_fiche_loc_items['coef']).""; if ($ligne_look_fiche_loc_items['rabais']) {print " - <i>".$ligne_look_fiche_loc_items['rabais']."%</i>";} print"</td><td><span class=\"";
		if ($ligne_look_fiche_loc_items['service']) {print "fact_service";}
		elseif ($ligne_look_fiche_loc_items['sous_traitance']) {print "fact_str";}
		else {print "fact_descr";}
		 
		print "\">".$ligne_look_fiche_loc_items['libelle']."</span>";
		print "</td>";
		print" <td>";
		if (!$ligne_look_fiche_loc_items['service'] && $ligne_look_fiche_loc_items['prix']>=0)
			{
			print"<img src=\"../images/coche_1.gif\">";
			for ($i = 1; $i <= $ligne_look_fiche_loc_items['quantite']; $i++) 
				{
				print"<img src=\"../images/coche_2.gif\">";
				if (!bcmod($i,8) && $i<$ligne_look_fiche_loc_items['quantite'])
					{
					print "<br>";
					print"<img src=\"../images/coche_1.gif\">";
					}
				}
			}
		print "</td>";
		print" <td>";
		if (!$ligne_look_fiche_loc_items['service'] && $ligne_look_fiche_loc_items['prix']>=0)
			{
			print"<img src=\"../images/coche_1.gif\">";
			for ($i = 1; $i <= $ligne_look_fiche_loc_items['quantite']; $i++) 
				{
				print"<img src=\"../images/coche_2.gif\">";
				if (!bcmod($i,8) && $i<$ligne_look_fiche_loc_items['quantite'])
					{
					print "<br>";
					print"<img src=\"../images/coche_1.gif\">";
					}
				}
			}
		print "</td>";
	
		print "<td align=\"right\">".bcadd($ligne_look_fiche_loc_items['prix'],0,2)." Fr</td>
		<td align=\"right\">".bcadd($prix,0,2)." Fr</td></tr>";
		}
	$prix_tot=$prix_tot+$prix;
		//sous traitance
	if ($ligne_look_fiche_loc_items['sous_traitance'])
		{
		$prix_str=$prix_str+$prix;
		}
	//services
	elseif ($ligne_look_fiche_loc_items['service'])
		{
		$prix_ser=$prix_ser+$prix;
		}

	}
?>
<tr>
   <td > </td>
    <td > </td>
	
	   <td> </td>
       <td></td>
    <td align="right" bgcolor="#EEEEEE">s.total</td>
    <td align="right" bgcolor="#EEEEEE"><? echo bcadd(round($prix_tot,1),0,2); ?> Fr</td>
  </tr>
<?
// ajout lighe rabais commun
if ((float)$ligne_look_fiche_loc['rabais'] OR (float)$ligne_look_fiche_loc['rabais_hc'] OR (float)$ligne_look_fiche_loc['rabais_djr'])
{ 
$rabais=$ligne_look_fiche_loc['rabais']+$ligne_look_fiche_loc['rabais_hc']+$ligne_look_fiche_loc['rabais_djr'];
$rabais_abs=bcadd(round($rabais*$prix_tot/100,1),0,2);


echo "<tr>
   <td></td>
    <td></td>
   <td></td>
    <td></td>
    <td align=\"right\">rabais $rabais%</td>
    <td align=\"right\">-$rabais_abs Fr</td>
  </tr>";
}
// ajout lighe ajustement
if ((float)$ligne_look_fiche_loc['ajustement'])
{ 
echo "
  <tr>
   <td></td>
    <td></td>
	   <td></td>
    <td></td>

    <td align=\"right\">Arrondi</td>
    <td align=\"right\">".bcadd($ligne_look_fiche_loc['ajustement'],0,2)." Fr</td>
  </tr>";
}
?>
	<? $prix_fact=$prix_tot+$ligne_look_fiche_loc['ajustement']-$rabais_djr_abs-$rabais_hc_abs-$rabais_abs; ?>

  <tr>
   <td></td>
    <td ><em>+ 3% en cas paiement VISA, MC total: </em></td>
   <td><em><? echo bcadd(round($prix_fact*1.03,1),0,2); ?> Fr</em></td>
    <td></td>
    <td align="right" bgcolor="#CCCCCC"><b>TOTAL</b></td>
    <td align="right" bgcolor="#CCCCCC"><b><? echo bcadd(round($prix_fact,1),0,2); ?> Fr</b></td>
  </tr>
</table>
<br><img src="../images/separator.gif" width="500" height="2"><br>
<table width="630" height="0" border="0">
		<tr>
				<td width="1"><img src="../images/seppvert.gif" width="2" height="100"></td>
				<td width="160" align="left" valign="top" class="Style1" >ACC<br>
				  ___
								  M/DSub - SX - SXa - Xa <br>
				___  RAKO acc.<br>
				___ chariot RAKO - pliable<br>
				LIGHT<br>
				___ g&eacute;latine(s - coque 36 <br>
				___ planchette<br>
				___ __________________</td>
				<td width="20" align="left" valign="top" class="Style1" ><br>
				  ___
				___<br>
				___<br>
				<br>
				___<br>
		  ___<br>___</td>
		        <td width="1" align="left" valign="top" class="Style1" ><img src="../images/seppvert.gif" width="2" height="100"></td>
		        <td width="160" align="left" valign="top" class="Style1">ELEC<br>
		          ___ rallonge &eacute;lectrique<br>
		          ___ enrouleur &eacute;lectrique		          <br>
___ triple fiche<br>
___ multiprise	            <br>
		          AUDIO<br>
	            ___
		        				rallonge XLR <br>
				___ cable iPod </td>
				<td width="20" align="left" valign="top" class="Style1" >___
				___<br>
				___<br>___<br>
				<br>
				___<br>				
				___<br>
				___</td>
		        <td width="1" class="Style1"><img src="../images/seppvert.gif" width="2" height="100"> </td>
		        <td width="250" valign="top" class="Style1"><br>
		          ___ __________________ ___<br>	            <br>
	            ___ __________________ ___<br>	            <br>
	            ___ __________________ ___<br>	            <br>
          ___ __________________ ___</td>
		        <td width="1"><img src="../images/seppvert.gif" width="2" height="100"></td>
		</tr>
</table>
<br><img src="../images/separator.gif" width="500" height="2"><br>

<?
$sql_update_fiche_loc_items="UPDATE fiches_loc SET  pour_soustraitance='$prix_str',  pour_service='$prix_ser',  prix_tot='$prix_tot', prix_fact='$prix_fact' WHERE id='".$ligne_look_fiche_loc['id']."'";
mysql_query($sql_update_fiche_loc_items,$db);
?>
<table border="0" cellpadding="10">
<?
$sql_journal="SELECT * FROM cpt_journal WHERE id_fiche_loc='".$ligne_look_fiche_loc['id']."' AND no_compte_credit LIKE '1%' ORDER BY date";
$res_journal=mysql_query($sql_journal,$db);
while($ligne_journal=mysql_fetch_object($res_journal))
	{
	// recherche des libelleés des comptes
	$sql_credit="SELECT * FROM cpt_comptes WHERE no='$ligne_journal->no_compte_credit'";
	$res_credit=mysql_query($sql_credit,$db);
	$ligne_credit=mysql_fetch_object($res_credit);
	$sql_debit="SELECT * FROM cpt_comptes WHERE no='$ligne_journal->no_compte_debit'";
	$res_debit=mysql_query($sql_debit,$db);
	$ligne_debit=mysql_fetch_object($res_debit);
	print "<tr>
	<td>".format_date($ligne_journal->date,0)."</td>
	<td>$ligne_journal->libelle, $ligne_credit->libelle_public -&gt; $ligne_debit->libelle_public </td>
	<td align=right>$ligne_journal->somme Fr</td>
	<td>__________________ "; 
	if ($ligne_journal->libelle=="retour caution" OR $ligne_journal->libelle=="remboursement")
		{
		$caution_retour=TRUE;
		print "(Client)";
		}
	else
		{
		print "(DJ-R)";
		}
	print "</td></tr>";
	}
if (!$caution_retour)
	{
	print "<tr>
	<td>___.___.______</td>
	<td>retour caution<input type=\"checkbox\"></td>
	<td align=right>________ Fr</td>
	<td>_________________________ (client)</td>
	</tr>";
	}
	
  
  ?>
</table>
  <? 
  if ($ligne_look_fiche_loc['etat']<=3) 
	  {
	  print "<br><img src=\"../images/separator.gif\" width=\"500\" height=\"2\"><br>A payer le "; 
	  print format_date($ligne_look_fiche_loc['date_out'],0); 
	  print ": <b>";
	  print $prix_fact+$ligne_look_fiche_loc['caution']+$ligne_look_fiche_loc['solde_en_notre_faveur'];
	  print " Fr</b><br>Dont ".$ligne_look_fiche_loc['caution']." Fr de caution qui vous seront restitués selon les conditions générales de DJ-R<br>"; 
	  }

  ?> <img src="../images/separator.gif" width="500" height="2"><br>
  <? if ($ligne_look_fiche_loc['etat']>=2) print'
  <input type="checkbox" name="checkbox2" value="checkbox">
  d&eacute;fauts constat&eacute;s: _______________________________________________________<br>
    <input type="checkbox" name="checkbox" value="checkbox">
Mat&eacute;riel revenu et v&eacute;rifi&eacute; Signature_______________________ (DJ-R) <br>
  <img src="../images/separator.gif" width="500" height="2"><br><table width="630">'; ?>
  		<tr>
  				<td align="right">je sousign&eacute;, <? echo $ligne_look_contact['titre']." ".$ligne_look_contact['nom']." ".$ligne_look_contact['prenom']; ?>,
  						acc&egrave;pte les conditions g&eacute;n&eacute;rales en vigueur<br>
  		<br>
  		<br>
		Gen&egrave;ve, le___.___.______
		Signature_______________________ </td>
  		</tr></table>

</body>
</html>